import React, { Component } from 'react'

export default class LoadingBar extends Component {
  constructor() {
    super()
    this.state = {
      percentage: 0,
    }
    this.startTimer = this.startTimer.bind(this)
  }
  componentDidMount() {
    this.startTimer()
  }
  componentWillUnmount() {
    clearInterval(this.timer)
  }
  render() {
    const { percentage } = this.state
    return (
      <div className={`rt-LoadingBar-container`}>
        <div className="-wrapper">
        	<div className="load-bar">
        		<div
              className={`load-bar-inner`}
            >
              <span>
                {percentage}%
              </span>
            </div>
        	</div>
        </div>
      </div>
    )
  }
  startTimer() {
    clearInterval(this.timer)
    this.timer = setInterval(() => {
      const { percentage } = this.state
      let newVal = 1 + percentage
      if (newVal > 100) {
        newVal = 1
      }
      this.setState({percentage: newVal})
    }, 100)
  }
}